import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from './../components/login/Login.vue'
import Register from './../components/register/Register.vue'
import Addinfo from './../components/addInfo/AddInfo.vue'
import Main from './../components/main/Main.vue'
import Details from './../components/main/details/Details.vue'
import Edit from './../components/edit/Edit.vue'
import Backstage from './../components/main/backstage/Backstage.vue'
// import GDMap from './../views/GDmap.vue'
Vue.use(VueRouter)

const routes = [{
        path: '/',
        component: Login,
        name: 'login'

    },
    // 这个模块会用vuex
    {
        path: '/gdmap',
        name: 'gdmap',
        component: () =>
            import ('./../views/GDmap.vue')

    },
    {
        path: '/backstage',
        name: 'backstage',
        component: Backstage
    },
    {
        path: '/edit',
        component: Edit,
        name: 'edit'
    },
    {
        path: '/register',
        name: 'register',
        component: Register
    },
    {
        path: '/addinfo',
        component: Addinfo
    },
    {
        path: '/main',
        name: 'main',
        component: Main,
        children: [{
            path: '/details/:id',
            name: 'details',
            component: Details
        }, ]
    },

]

const router = new VueRouter({
        routes,
        mode: 'history'
    })
    // 全局前置守卫
router.beforeEach((to, from, next) => {
    // 未登录时，中断当前导航，跳转到登陆页面
    if (!localStorage.getItem('20') && to.name != 'register') { // 排除要跳转的页面
        next({
            // 中断
            replace: true,
            name: 'register'
        });
    } else {
        next();
    }
});

export default router